require 'View/UIBase/UIBase'

UICharactorInfoHeroRune = UIBase.New(PanelNames.UICharactorInfoHeroRune)

local commonRuneCount = 6   --符文
local specialRuneIndex = 7  --天穹

function UICharactorInfoHeroRune.Init(partnerId)
    UICharactorInfoHeroRune.partnerId = partnerId
    UIBase.Init(UICharactorInfoHeroRune)
end

function UICharactorInfoHeroRune:OnCreate()
    self.btnSkill = self:GetChild("SkillButton1")
    self.txtSkillName = self:GetChild("SkillName")
    self.txtSkillDesc = self:GetChild("SkillDes")
    self.imgSkill = self:GetChild("SkillButton1"):GetChild("Mask"):GetChild("Icon")
    self.txtSkillLock1 = self:GetChild("SkillLock1")
    self.txtSkillTips = self:GetChild("Text_activation")
    self.helpBtn = self:GetChild("help")
    self.btnRuneList = {}
    for i= 1,specialRuneIndex do
        local btnCommonRune = self:GetChild("Btn_Rune_"..i)
        table.insert(self.btnRuneList, btnCommonRune)
    end
    --self.btnSpecialRune = self:GetChild("Btn_Rune_"..specialRuneIndex)
    self.ctrlShow = self:GetController("show")
    self.ctrlHave = self:GetController("Rune")
    self.ctrlActive = self:GetController("activation")
    self:AddEvent()
    self:Refresh()
end

function UICharactorInfoHeroRune:OnEnable()
    self:Refresh()
end

function UICharactorInfoHeroRune:AddEvent()
    self:RegisterEvent(Config.EventType.FreshHeroRune,self.Refresh)
    self:RegisterEvent(Config.EventType.FreshHeroRuneExchange,self.Refresh)
    self:RegisterEvent(Config.EventType.FreshHeroRuneLevelUp,self.Refresh)
    self:RegisterOnClick(self.helpBtn,self.OnClickHelp)
end

function UICharactorInfoHeroRune:Refresh()
    self:RefreshCommonRune()
    self:RefreshSkill()
    for i= 1,specialRuneIndex do
        UIRedDots.addViewListen(PanelNames.UICharactorInfoHeroRune, self.fguiObj.name .. self.btnRuneList[i].name, self.btnRuneList[i], Config.RedDotsType.RuneWear, self.partnerId, i-1)
    end
end

function UICharactorInfoHeroRune:RefreshCommonRune()
    for i = 1,7 do
        local btnCommonRune = self.btnRuneList[i]
        if btnCommonRune then
            btnCommonRune.onClick:Clear()
            local runeInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId, i-1)
            logicMgr.RuneManager.SetHeroRuneItemData(btnCommonRune, runeInfo, self.partnerId, i-1)
            local isLock = logicMgr.RuneManager.CheckRuneLock(self.partnerId,i-1)
            if not isLock then
                if runeInfo then
                    btnCommonRune.onClick:Add(function()
                        local curRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId, i-1)
                        showUI(PanelNames.UIRuneInfo,curRuneInfo)
                    end)
                else
                    btnCommonRune.onClick:Add(function()
                        showUI(PanelNames.UIRuneList, self.partnerId, i-1)
                    end)
                end
            end
        end
    end
end

function UICharactorInfoHeroRune:RefreshSkill()
    self.imgSkill.url = ""
    local specialRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId, commonRuneCount)
    if specialRuneInfo then
        local suitCfg = nil
        local newRuneCfg = nil
        local flag = false
        local showNext = false
        local oldCount = 0
        local newCount = 0
        local nextCount = 0
        local nextQualityCfg = 0
        self.ctrlHave.selectedIndex = 1
        local isActive, info1, info2, info3 = logicMgr.RuneManager.CheckSpecialRuneSkillActive(self.partnerId)
        suitCfg = dataTable.getDataTableByStringId("t_runesuit",specialRuneInfo.itemModelId)
        if isActive then
            for k,v in pairs(info2) do
                if v.quality >= specialRuneInfo.quality then
                    oldCount = oldCount + 1
                end
            end
            self.ctrlActive.selectedIndex = 0
            if info2[info1].quality < specialRuneInfo.quality then
                flag = true
                local showQuality = info2[info1].quality
                local newSpecialModelId = logicMgr.RuneManager.GetShowSpecialRune(specialRuneInfo.itemModelId, showQuality)
                if newSpecialModelId then
                    suitCfg = dataTable.getDataTableByStringId("t_runesuit",newSpecialModelId)
                    newRuneCfg = dataTable.getDataTableByStringId("t_rune",newSpecialModelId)
                end
                for k,v in pairs(info2) do
                    if v.quality >= showQuality then
                        newCount = newCount + 1
                    end
                end
            end
            nextQualityCfg = logicMgr.RuneManager.GetNextQualitySpecialRune(info2[info1].itemModelId, info2[info1].quality)
            if nextQualityCfg and nextQualityCfg.f_Quality <= specialRuneInfo.quality then
                showNext = true
                for k,v in pairs(info2) do
                    if v.quality >= nextQualityCfg.f_Quality then
                        nextCount = nextCount + 1
                    end
                end
            end
            local txt2 = ""
            if flag then
                local newQualityName =  EnumConst.StarType[newRuneCfg.f_Quality]
                txt2 = newQualityName.."("..newCount.."/"..info1..")"
            end
            local txt3 = ""
            if showNext then
                local nextQualityName =  EnumConst.StarType[nextQualityCfg.f_Quality]
                txt3 = nextQualityName.."("..nextCount.."/"..info1..")"
            end
            local oldQualityName =  EnumConst.StarType[specialRuneInfo.quality]
            local txt1 = oldQualityName.."("..oldCount.."/"..info1..")"
            if showNext then
                if flag then
                    self.txtSkillTips.text = string.format(getLanguage("RuneRule2"), txt2, txt3)
                else
                    self.txtSkillTips.text = string.format(getLanguage("RuneRule2"), txt1, txt3)
                end
            else
                if flag then
                    self.txtSkillTips.text = string.format(getLanguage("RuneRule3"), txt2)
                else
                    self.txtSkillTips.text = string.format(getLanguage("RuneRule3"), txt1)
                end
            end
        else
            self.ctrlActive.selectedIndex = 1
            self.txtSkillLock1.text = string.format(getLanguage("RuneSkillActive"), info1, info2, info3)
        end
        local skillInfo = dataTable.getDataTableByStringId("t_skillshow", suitCfg.f_Effect)
        if skillInfo then
            self.imgSkill.url = "ui://SkillAtlas/" .. skillInfo.f_SkillIcon
        end
        self.txtSkillName.text = getLanguage(suitCfg.f_RuneSuitName)
        self.txtSkillDesc.text = getLanguage(suitCfg.f_EffectDesc)
    else
        self.ctrlHave.selectedIndex = 0
    end
end

function UICharactorInfoHeroRune:OnClickHelp()
    local txts = {}
    txts["title"] = "RuneRuleTitle"
    txts["des"] = "RuneRule"
    showUI(PanelNames.CommonTips, txts)
end

function UICharactorInfoHeroRune:OnDisable()
    UIRedDots.removeViewEvent(PanelNames.UICharactorInfoHeroRune)
end

return UICharactorInfoHeroRune